import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@//utils/token'
// 注意import要放到最上面
// import login from '../views/login'
// import layout from '../views/layout'
// // 导入layout嵌套路由的子组件
// import chart from '@/views/layout/chart'
// import enterprise from '@/views/layout/enterprise'
// import question from '@/views/layout/question'
// import subject from '@/views/layout/subject'
// import user from '@/views/layout/user'
// import welcome from '@/views/layout/welcome'

// 路由懒加载
const login = () => import('@/views/login')
const layout = () => import('@/views/layout')
// layoutd的子组件
const chart = () => import('@/views/layout/chart')
const enterprise = () => import('@/views/layout/enterprise')
const question = () => import('@/views/layout/question')
const subject = () => import('@/views/layout/subject')
const user = () => import('@/views/layout/user')
const welcome = () => import('@/views/layout/welcome')

// 练习插槽导入的组件
// import father from '@/slot/defaultSlot/father.vue'
// // 具名插槽
// import parents from '@/slot/nameSlot/parents.vue'
// 按需导入

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/layout',
    name: 'layout',
    component: layout,
    children: [
      // 子路由
      // 子路由中的path前面不要加/，如果加了 /layout 会自动被干掉
      {
        path: 'welcome',
        component: welcome,
        meta: {
          title: '欢迎用户',
          icon: 'el-icon-service',
          path: '/layout/welcome',
          rules: ['超级管理员', '管理员', '老师', '学生']
        }
      },
      {
        path: 'chart',
        component: chart,
        meta: {
          title: '数据浏览',
          icon: 'el-icon-pie-chart',
          path: 'layout/chart',
          rules: ['超级管理员', '管理员', '老师']
        }
      },
      {
        path: 'enterprise',
        component: enterprise,
        meta: {
          title: '企业列表',
          icon: 'el-icon-office-building',
          path: '/layout/enterprise',
          rules: ['超级管理员', '管理员', '老师']
        }
      },
      {
        path: 'question',
        component: question,
        meta: {
          title: '题库列表',
          icon: 'el-icon-edit-outline',
          path: '/layout/question',
          rules: ['超级管理员', '管理员', '老师', '学生']
        }
      },
      {
        path: 'subject',
        component: subject,
        meta: {
          title: '学科列表',
          icon: 'el-icon-pie-chart',
          path: '/layout/subject',
          rules: ['超级管理员', '管理员', '老师']
        }
      },
      {
        path: 'user',
        component: user,
        meta: {
          title: '用户列表',
          icon: 'el-icon-user',
          path: '/layout/user',
          rules: ['超级管理员', '管理员', '老师']
        }
      }
    ]
  }
  // 以下是练习插槽,默认插槽
  // {
  //   path: '/father',
  //   component: father
  // },
  // {
  //   path: '/parents',
  //   component: parents
  // }
]

const router = new VueRouter({
  routes
})
// 导航守卫
router.beforeEach((to, from, next) => {
  console.log(to)
  if (to.path === '/login') {
    next()
  } else {
    const token = getToken()
    // console.log(token)

    if (token) {
      // if (router.options.routes[2].children)
      next()
    } else {
      // console.log('没有token')
      next('/login')
    }
  }
})
// 导航后置守卫
router.afterEach((to, from) => {
  // console.log(to)
  document.title = to.meta.title || '黑马面面'
})

export default router
